package ar.uba.fi.tonyvaliente.tree;

import java.util.NoSuchElementException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Implementación en memoria del árbol.
 * 
 * @author santiago
 *
 */
public class MemoryBTree<K extends Comparable<K>, V> extends BTree<K, V> {

	private static Log log = LogFactory.getLog(MemoryBTree.class);

	public MemoryBTree() throws BTreeException {
		super();
		BReference<K, V> reference = this.createReference();
		reference.setNode(new BTreeNode<K, V>(this));
		this.rootNode = reference.getNode();
		this.rootNode.setThisReference(reference);
		this.rootNode.setTree(this);
	}
	
	public MemoryBTree(Integer elementosPorNodo) throws BTreeException {
		super();
		BReference<K, V> reference = this.createReference();
		reference.setNode(new BTreeNode<K, V>(this/*, elementosPorNodo*/));
		this.rootNode = reference.getNode();
		this.rootNode.setThisReference(reference);
		this.rootNode.setTree(this);
	}



	/* (non-Javadoc)
	 * @see ar.uba.fi.datos.tonyvaliente.tree.BTree#createBReference()
	 */
	@Override
	protected BReference<K, V> createReference() {

		BReference<K, V> reference = new MemoryBReference<K, V>();
		return reference;
	}

	/* (non-Javadoc)
	 * @see ar.uba.fi.datos.tonyvaliente.tree.BTree#get(java.lang.Comparable)
	 */
	@Override
	public V get(K key) throws BTreeException, NoSuchElementException {
		return this.rootNode.get(key);
	}

	@Override
	public void close() {
		// do nothing
	}
	
	protected void setRootNode(BReference<K, V> rootRef) throws BTreeException {
		rootNode = rootRef.getNode();
	}
}
